import 'dart:ffi';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:t2app/src/common/AppData.dart';
import 'package:t2app/src/entity/User.dart';
import 'package:t2app/src/util/log_util.dart';
import 'package:t2app/src/util/sp_util.dart';

import '../../Config.dart';
import '../../common/Tool.dart';

class MyView extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new MyViewState();
  }
}

class MyViewState extends State<MyView> {
  User? _user;

  Future<void> _init() async {
    setState(() {
      _user = AppData.user;
      LogUtil.p("--------------------");
    });
  }

  @override
  void initState() {
    super.initState();
    _init();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("软件万事屋"),
        backgroundColor: Config.THEME.primaryColorLight,
        actions: [
          if (_user != null)
            IconButton(
              icon: Icon(Icons.exit_to_app, color: Colors.grey, size: 30),
              onPressed: () {
                // 注销
                // 1. 清空缓存
                // 2. 清空 appdata
                // 跳转首页
                SpUtil.instance.removeKey("user");
                AppData.user = null;
                setState(() {
                  _user = null;
                });
                Tool.EasyLoading_Success("注销成功");
              },
            ),
          if (_user == null)
            IconButton(
              icon: Icon(Icons.login_sharp, color: Config.THEME.primaryColor,
                  size: 30),
              onPressed: () {
                Navigator.of(context).pushNamed('loginIndex').then((bool) {
                  _init();
                });
              },
            ),
        ],
      ),
      body: RefreshIndicator(
          onRefresh: _init,
          child: SingleChildScrollView(
          physics: const AlwaysScrollableScrollPhysics(),
          child:  new Container(
                    child: new Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        new Container(
                          child: new Row(
                            mainAxisAlignment: MainAxisAlignment.start,
                            children: [
                              // 左侧 logo，使用 Container 的 margin 属性设置外边距
                              Container(
                                margin: const EdgeInsets.all(20.0),
                                child: ClipOval(
                                  child: new CachedNetworkImage(
                                    width: 100,
                                    height: 100,
                                    imageUrl: _user?.head_img ?? Config.LOGO,
                                  ),
                                ),
                              ),
                              // 右侧用户信息
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  // 用户名称和用户编号
                                  Column(
                                    crossAxisAlignment: CrossAxisAlignment.start,
                                    children: [
                                      new Text(
                                        (_user?.nickname ?? "未登录"),
                                        style: TextStyle(fontSize: 30,
                                            color: Config.THEME.primaryColorDark),
                                      ),
                                      SizedBox(width: 10),
                                      new Text(
                                        '编号:' + (_user?.user_sn ?? "暂无编号"),
                                        style: TextStyle(fontSize: 15, color: Colors.white),
                                      ),
                                    ],
                                  ),
                                  // VIP 信息
                                  if (_user?.isVip() == true)
                                    Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        Padding(
                                          padding: const EdgeInsets.only(top: 8.0),
                                          child: Container(
                                            padding: EdgeInsets.symmetric(
                                                horizontal: 8, vertical: 4),
                                            decoration: BoxDecoration(
                                              color: Colors.yellow,
                                              borderRadius: BorderRadius.circular(4),
                                            ),
                                            child: Text(
                                              "VIP",
                                              style: TextStyle(
                                                  color: Colors.black,
                                                  fontWeight: FontWeight.bold),
                                            ),
                                          ),
                                        ),
                                        SizedBox(width: 10),
                                        Text(
                                          "到期时间: ${_user?.expire_time}",
                                          style: TextStyle(color: Colors.white),
                                        ),
                                      ],
                                    ),
                                  if (_user?.isVip() != true)
                                    Padding(
                                      padding: const EdgeInsets.only(top: 8.0),
                                      child: ElevatedButton(
                                        onPressed: () {
                                          // 处理开通 VIP 逻辑
                                        },
                                        child: Text("开通 VIP"),
                                      ),
                                    ),
                                ],
                              ),
                            ],
                          ),
                          color: Config.THEME.primaryColorLight,
                          width: MediaQuery
                              .of(context)
                              .size
                              .width,
                          height: 200,
                        ),
                      ],
                    ),
                  ),
          )
      )
    );
  }
}